#!/bin/bash
#set -x
forever=`which forever`
log='8081.log'
prog=show.sh
RETVAL=0

frontail(){
    grep_frontail=`forever list | grep -v grep | grep "${log}"`
    if [ ${#grep_frontail} -gt 0 ]
    then
	echo " frontail"
	#`/usr/bin/forever stop index.js`
	#/usr/bin/forever stop index.js
	RETVAL=$?
    else
	echo "forever show is not running."
    fi
}
stop() {
    grep_show=`forever list | grep -v grep | grep "${log}"`
    echo  ${grep_show}
    if [ ${#grep_show} -gt 0 ]
    then
	echo "Stop show"
	#`/usr/bin/forever stop index.js`
	"$forever" stop index.js
	RETVAL=$?
    else
	echo "forever show is not running."
    fi
}
start() {
    grep_show=`forever list | grep -v grep | grep "${log}"`
    if [ -n "${grep_show}" ]
    then
	echo "forever show is already running."
    else
	echo "Start show"
	"$forever" start -al 8081.log index.js
	#`/usr/bin/forever start -al 8081.log index.js`
	#`frontail -U ccms -P ccms_123 -p 9002 /home/ec2-user/.forever/8081.log --fork`
	RETVAL=$?
    fi
}
restart(){
	"$forever" restart index.js
}
list(){
	forever list
	#`forever list`
	#`${forever} list`
}
case "$1" in
    list)
	list
	;;
    start)
	start
	;;
    stop)
	stop
	;;
    restart)
	stop
	start
	;;
    *)
	echo $"Usage: $prog {start|stop|restart|list}"
	exit 1
esac

exit $RETVAL
